package com.hzdesk.media.koreabatchmatvector.request;

import android.content.Context;
import android.util.Log;


import com.hzdesk.media.koreabatchmatvector.inter.ICallBack;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2018/5/29.
 */

public class UploadImageRequest {
    private static final String TAG = UploadImageRequest.class.getSimpleName();
    private Model model = Model.getInstance();
    private Context context;
    private String Url;

    public UploadImageRequest(Context context, String url) {
        this.context = context;
        Url = url;
        Log.d(TAG,url);
    }

    public interface OnUploadImageListener {
        void success();

        void fail(String s);
    }

    private OnUploadImageListener onUploadImageListener;
    private String Success = "SUCCESS";
    private String Fail = "FAIL";

    public void setOnUploadImageListener(OnUploadImageListener onUploadImageListener) {
        this.onUploadImageListener = onUploadImageListener;
    }

    public void setRequestBody(String path) {
        Log.d(TAG, path);
        File file=new File(path);
        if (file!=null){
            postRequest(Url, file);
        }
    }


    private void postRequest(String url, File file) {
       /* model.uploadImage(context, url, requestBody, new ICallBack() {
            @Override
            public void result(String s) {
                Log.d(TAG,s);
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    if (jsonObject.has("message")) {
                        if (jsonObject.getString("message").equals(Success)) {
                            onUploadImageListener.success();
                        }
                        if (jsonObject.getString("message").equals(Fail)) {
                            onUploadImageListener.fail(s);
                        }
                    }
                } catch (JSONException e) {
                    //e.printStackTrace();
                    Log.d(TAG,s);
                }

            }
        });*/

       model.UploadImageByMap(context, url, file, new ICallBack() {
           @Override
           public void result(String s) {
               Log.d(TAG,s);

           }
       });


    }
}
